#include "StdAfx.h"
#include "dsegen.h"
#include "DsegenTypes.h"
#include <assert.h>

#ifdef WIN32
#include <stdio.h>
#include "io.h"
#include "fcntl.h"
#include "direct.h"
#include <limits.h>
#else
#include <linux/limits.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include "unistd.h"
#include "dirent.h"
#endif

namespace dhis
{
	//--------------------------------------------------------------------------
	// Represents a rectangle in a 2D coordinate system (integer coordinates)
	//--------------------------------------------------------------------------

	CRectI::CRectI()
	{
		X = Y = Width = Height = 0;
	}

	CRectI::CRectI( int x,
		int y,
		int width,
		int height)
	{
		X = x;
		Y = y;
		Width = width;
		Height = height;
	}

	CRectI* CRectI::Clone() const
	{
		return new CRectI(X, Y, Width, Height);
	}

	void CRectI::GetBounds( CRectI* rect) const
	{
		rect->X = X;
		rect->Y = Y;
		rect->Width = Width;
		rect->Height = Height;
	}

	int CRectI::GetLeft() const
	{
		return X;
	}

	int CRectI::GetTop() const
	{
		return Y;
	}

	int CRectI::GetRight() const
	{
		return X+Width;
	}

	int CRectI::GetBottom() const
	{
		return Y+Height;
	}

	bool CRectI::IsEmptyArea() const
	{
		return (Width <= 0) || (Height <= 0);
	}

	bool CRectI::Equals( const CRectI & rect) const
	{
		return X == rect.X &&
			Y == rect.Y &&
			Width == rect.Width &&
			Height == rect.Height;
	}

	bool CRectI::Contains( int x,int y) const
	{
		return x >= X && x < X+Width &&
			y >= Y && y < Y+Height;
	}

	bool CRectI::Contains( CRectI& rect) const
	{
		return (X <= rect.X) && (rect.GetRight() <= GetRight()) &&
			(Y <= rect.Y) && (rect.GetBottom() <= GetBottom());
	}

	void CRectI::Inflate( int dx,  int dy)
	{
		X -= dx;
		Y -= dy;
		Width += 2*dx;
		Height += 2*dy;
	}

	bool CRectI::Intersect( const CRectI& rect)
	{
		return Intersect(*this, *this, rect);
	}

	void CRectI::Offset( int dx, int dy)
	{
		X += dx;
		Y += dy;
	}

	bool CRectI::Intersect( CRectI& c, const CRectI& a, const CRectI& b)
	{
		int right = min(a.GetRight(), b.GetRight());
		int bottom = min(a.GetBottom(), b.GetBottom());
		int left = max(a.GetLeft(), b.GetLeft());
		int top = max(a.GetTop(), b.GetTop());

		c.X = left;
		c.Y = top;
		c.Width = right - left;
		c.Height = bottom - top;
		return !c.IsEmptyArea();
	}

	bool CRectI::IntersectsWith( const CRectI& rect) const
	{
		return (GetLeft() < rect.GetRight() &&
			GetTop() < rect.GetBottom() &&
			GetRight() > rect.GetLeft() &&
			GetBottom() > rect.GetTop());
	}

	bool CRectI::Union( CRectI& c,  const CRectI& a, const CRectI& b)
	{
		int right = max(a.GetRight(), b.GetRight());
		int bottom = max(a.GetBottom(), b.GetBottom());
		int left = min(a.GetLeft(), b.GetLeft());
		int top = min(a.GetTop(), b.GetTop());

		c.X = left;
		c.Y = top;
		c.Width = right - left;
		c.Height = bottom - top;
		return !c.IsEmptyArea();
	}

}
